package com.example.seakdreamandroid.lottery;

import static com.example.seakdreamandroid.Constant.BASE_URL;
import static com.mob.MobSDK.getContext;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.seakdreamandroid.Constant;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.accountInfo.AccountInfoActivity;
import com.example.seakdreamandroid.chatbot.ChatbotActivity;
import com.example.seakdreamandroid.entity.User;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Lottery extends AppCompatActivity {
    User user;
    private LuckDrawView mLuckDrawView;
    private TextView points;
    private String myPoints;
    private ImageView back;
    List<PrizeListBean> prizeListBeanList = new ArrayList<PrizeListBean>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lottery);
        mLuckDrawView = findViewById(R.id.luck_draw);
        points = findViewById(R.id.points);
        user = (User)getContext().getApplicationContext();
        setPoints();

        back = findViewById(R.id.lottery_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mLuckDrawView.setOnItemListener(new LuckDrawView.OnItemListener() {
            @Override
            public void onClick() {//开始抽奖

                int j = Integer.parseInt(myPoints);
                if(j<60){
                    Toast.makeText(Lottery.this, "您的积分不足", Toast.LENGTH_SHORT).show();
                }else {
                    int stayIndex = getGood();
                    Log.e("ww", String.valueOf(stayIndex));
                    mLuckDrawView.startGame(stayIndex);
                    int i = j-60;
                    myPoints = String.valueOf(i);
                    points.setText(myPoints);
                    setNewPoints(i);
                }
            }

            @Override
            public void onShop(int Index) {//动画停止
                Toast.makeText(Lottery.this, "恭喜您获得："+prizeListBeanList.get(Index).getGoodName(), Toast.LENGTH_SHORT).show();
            }
        });
        mLuckDrawView.setData(getLuckDrawData());//设置奖品数据
    }
    public void setPoints(){
        String userId = user.getUserId();
        Request request = new Request.Builder()
                //设置请求的地址，默认的请求方式是Get请求
                .url(BASE_URL+"UserInfo/getUserInfo"+"?id="+userId)
                //创建Request对象
                .build();
        //3.发送请求，获取响应
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        //发起异步的请求，不需要手动创建子线程
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败时回调
                Log.e("MineActivity","onFailure，请求数据失败");
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Lottery.this,"网络故障，请检查网络",Toast.LENGTH_LONG).show();

                    }
                });
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                //请求成功时回调
                Log.i("MineActivity","onResponse"+"成功");
                try {
                    String resultStr = response.body().string();
                    JSONObject object = new JSONObject(resultStr);
                    JSONObject data = object.getJSONObject("data");
                    if(!data.isNull("points")){
                        myPoints = data.getString("points");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                points.setText(myPoints);
                            }
                        });

                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
                //setview
            }
        });

    }
    public void setNewPoints(int points){
        FormBody body = new FormBody.Builder()
                .add("userId",user.getUserId())
                .add("points", String.valueOf(points))
                .build();
        //3.创建请求对象（Request）
        Request request = new Request.Builder()
                //设置
                .url(Constant.BASE_URL + "UserInfo"+"/updatePoints")
                .post(body)//设置请求方式为POST，添加了请求体
                //创建Request对象
                .build();
        //4.发送请求，获取响应
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("msg","失败");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("msg","成功");

                    }
                });
            }
        });
    }
    public int getGood(){
        int index = new Random().nextInt(1000);
        if(index==0){
            return 0;
        }else if(index == 1){
            return 2;
        }
        else if(index == 2){
            return 3;
        }
        else if(index == 3){
            return 4;
        }
        else if(index == 4){
            return 6;
        }
        else if(index == 5) {
            return 7;
        }else if(index>=6 && index <500){
            return 1;
        }else {
            return 5;
        }

    }

    public List<PrizeListBean> getLuckDrawData(){

        PrizeListBean prizeListBean1 = new PrizeListBean();
        PrizeListBean prizeListBean2 = new PrizeListBean();
        PrizeListBean prizeListBean3 = new PrizeListBean();
        PrizeListBean prizeListBean4 = new PrizeListBean();
        PrizeListBean prizeListBean5 = new PrizeListBean();
        PrizeListBean prizeListBean6 = new PrizeListBean();
        PrizeListBean prizeListBean7 = new PrizeListBean();
        PrizeListBean prizeListBean8 = new PrizeListBean();

        prizeListBean1.setGoodName("AirPods");
        prizeListBean2.setGoodName("感谢参与");
        prizeListBean3.setGoodName("Mac");
        prizeListBean4.setGoodName("眼罩");
        prizeListBean5.setGoodName("耳塞");
        prizeListBean6.setGoodName("感谢参与");
        prizeListBean7.setGoodName("ipad");
        prizeListBean8.setGoodName("iPhone");

        prizeListBean1.setPrize_path("https://cdn.mos.cms.futurecdn.net/qMRagLMmCvMsHQHfvmWT2e.jpg");
        prizeListBean2.setPrize_path("https://bpic.588ku.com/element_origin_min_pic/19/06/04/b70e3a1950ccc8176afeed3e57156e41.jpg");
        prizeListBean3.setPrize_path("https://www.bhphotovideo.com/images/images2000x2000/apple_me086ll_a_21_5_imac_desktop_computer_1006607.jpg");
        prizeListBean4.setPrize_path("https://img.zcool.cn/community/0147925aa1f128a801206d96ce83b9.jpg@3000w_1l_2o_100sh.jpg");
        prizeListBean5.setPrize_path("https://cbu01.alicdn.com/img/ibank/2018/630/695/8665596036_1725547821.jpg");
        prizeListBean6.setPrize_path("https://bpic.588ku.com/element_origin_min_pic/19/06/04/b70e3a1950ccc8176afeed3e57156e41.jpg");
        prizeListBean7.setPrize_path("https://tse1-mm.cn.bing.net/th/id/OIP-C.VwhXPiDdQ96V_M5FmIgcYAHaEz?pid=ImgDet&rs=1");
        prizeListBean8.setPrize_path("https://ts1.cn.mm.bing.net/th/id/R-C.eae4036ec313289209578ffb6847103a?rik=4Qj32M1q4dbbag&riu=http%3a%2f%2fteepr.com%2fwp-content%2fuploads%2f2016%2f03%2f56e0e7377eac2.jpg&ehk=teDD3b51d16hKcCcn0r9WqWj1hMkHE0diuESKYb7Tgg%3d&risl=&pid=ImgRaw&r=0");



        prizeListBeanList.add(prizeListBean1);
        prizeListBeanList.add(prizeListBean2);
        prizeListBeanList.add(prizeListBean3);
        prizeListBeanList.add(prizeListBean4);
        prizeListBeanList.add(prizeListBean5);
        prizeListBeanList.add(prizeListBean6);
        prizeListBeanList.add(prizeListBean7);
        prizeListBeanList.add(prizeListBean8);

        return prizeListBeanList;

    }
}
